package com.tjec.project.businessWorkbench.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.tjec.common.enums.CatalogName;
import com.tjec.common.enums.CatalogType;
import com.tjec.common.utils.DateUtils;
import com.tjec.common.utils.ServletUtils;
import com.tjec.common.utils.StringUtils;
import com.tjec.framework.security.service.TokenService;
import com.tjec.framework.web.domain.TreeSelectExport;
import com.tjec.framework.web.domain.TreeSelectNew;

import com.tjec.framework.web.domain.TreeSelectProjectDoc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RestController;

import com.tjec.project.businessWorkbench.mapper.BmsProjectDocCatalogMapper;
import com.tjec.project.businessWorkbench.domain.BmsBusinessTask;
import com.tjec.project.businessWorkbench.domain.BmsProjectDocCatalog;
import com.tjec.project.businessWorkbench.domain.ImportBmsProjectDocCatalog;
import com.tjec.project.businessWorkbench.service.IBmsProjectDocCatalogService;

import net.bytebuddy.asm.Advice.Return;

/**
 * 项目文档目录Service业务层处理
 * 
 * @author pangyongfeng
 * @date 2020-04-26
 */
@Service
public class BmsProjectDocCatalogServiceImpl implements IBmsProjectDocCatalogService {
	@Autowired
	private BmsProjectDocCatalogMapper bmsProjectDocCatalogMapper;

	@Autowired
	private TokenService tokenService;

	/**
	 * 查询项目文档目录
	 * 
	 * @param guid 项目文档目录ID
	 * @return 项目文档目录
	 */
	@Override
	public BmsProjectDocCatalog selectBmsProjectDocCatalogByGuid(String guid) {
		return bmsProjectDocCatalogMapper.selectBmsProjectDocCatalogByGuid(guid);
	}

	/**
	 * 查询项目文档目录列表
	 * 
	 * @param bmsProjectDocCatalog 项目文档目录
	 * @return 项目文档目录
	 */
	@Override
	public List<BmsProjectDocCatalog> selectBmsProjectDocCatalogList(BmsProjectDocCatalog bmsProjectDocCatalog) {
		return bmsProjectDocCatalogMapper.selectBmsProjectDocCatalogList(bmsProjectDocCatalog);
	}
	
	/**
	 * 优化版-查询项目文档目录列表
	 * 
	 * @param bmsProjectDocCatalog 项目文档目录
	 * @return 项目文档目录
	 */
	@Override
	public List<BmsProjectDocCatalog> selectBmsProjectDocCatalogListNew(BmsProjectDocCatalog bmsProjectDocCatalog) {
		return bmsProjectDocCatalogMapper.selectBmsProjectDocCatalogListNew(bmsProjectDocCatalog);
	}
	
	/**
	 * 优化版-根目录-查询项目文档目录列表
	 * 
	 * @param bmsProjectDocCatalog 项目文档目录
	 * @return 项目文档目录
	 */
	@Override
	public List<BmsProjectDocCatalog> selectBmsProjectDocCatalogListFirstNew(BmsProjectDocCatalog bmsProjectDocCatalog) {
		return bmsProjectDocCatalogMapper.selectBmsProjectDocCatalogListFirstNew(bmsProjectDocCatalog);
	}

	/**
	 * 新增项目文档目录
	 * 
	 * @param bmsProjectDocCatalog 项目文档目录
	 * @return 结果
	 */
	@Override
	public int insertBmsProjectDocCatalog(BmsProjectDocCatalog bmsProjectDocCatalog) {
		List<BmsProjectDocCatalog> lists = bmsProjectDocCatalogMapper
				.selectBmsProjectDocCatalogByBusinessId(bmsProjectDocCatalog.getBusinessId());
		if (lists != null && lists.size() == 0) {
			return bmsProjectDocCatalogMapper.insertBmsProjectDocCatalog(bmsProjectDocCatalog);
		} else {
			return bmsProjectDocCatalogMapper.insertBmsProjectDocCatalog(bmsProjectDocCatalog);
		}
	}
	
	/**
	 * 新增项目文档目录
	 * 
	 * @param bmsProjectDocCatalog 项目文档目录
	 * @return 结果
	 */
	@Override
	public BmsProjectDocCatalog insertBmsProjectDocCatalogNew(BmsProjectDocCatalog bmsProjectDocCatalog) {
		
		/*List<BmsProjectDocCatalog> lists = bmsProjectDocCatalogMapper
				.selectBmsProjectDocCatalogByBusinessId(bmsProjectDocCatalog.getBusinessId());
		
		if (lists != null && lists.size() == 0) {
			bmsProjectDocCatalogMapper.insertBmsProjectDocCatalog(bmsProjectDocCatalog);
		} else {
			bmsProjectDocCatalogMapper.insertBmsProjectDocCatalog(bmsProjectDocCatalog);
		}*/
		
		bmsProjectDocCatalogMapper.insertBmsProjectDocCatalog(bmsProjectDocCatalog);
		return bmsProjectDocCatalog;
	}

	/**
	 * 修改项目文档目录
	 * 
	 * @param bmsProjectDocCatalog 项目文档目录
	 * @return 结果
	 */
	@Override
	public int updateBmsProjectDocCatalog(BmsProjectDocCatalog bmsProjectDocCatalog) {
		return bmsProjectDocCatalogMapper.updateBmsProjectDocCatalog(bmsProjectDocCatalog);
	}

	/**
	 * 删除项目文档目录信息
	 * 
	 * @param guid 项目文档目录ID
	 * @return 结果
	 */
	@Override
	public int deleteBmsProjectDocCatalogByGuid(String guid) {
		//待删除
		if (bmsProjectDocCatalogMapper.checkDocCounts(guid) > 0) {
			return -1;
		}
		BmsProjectDocCatalog bmsProjectDocCatalog = new BmsProjectDocCatalog();
		bmsProjectDocCatalog.setGuid(guid);
		bmsProjectDocCatalog.setDelFlag("1");
		bmsProjectDocCatalogMapper.updateBmsProjectDocCatalog(bmsProjectDocCatalog);
		return bmsProjectDocCatalogMapper.deleteBmsProjectDocCatalogByGuid(guid);
	}
	
	/**
	 * 删除项目文档目录信息
	 * 
	 * @param guid 项目文档目录ID
	 * @return 结果
	 */
	@Override
	public int removeCheck(String guid) {
		//查询被删除目录下的所有目录是否包含附件
		//如果包含附件则不能删除
		//否则可以删除
		if (bmsProjectDocCatalogMapper.checkDocCounts(guid) > 0) {
			return -1;
		} else {
			return 1;
		}

	}

	@Override
	public List<BmsProjectDocCatalog> buildDocCatalogTree(List<BmsProjectDocCatalog> bmsProjectDocCatalogs) {
		List<BmsProjectDocCatalog> returnList = new ArrayList<BmsProjectDocCatalog>();
		List<String> tempList = new ArrayList<String>();
		for (BmsProjectDocCatalog bmsProjectDocCatalog : bmsProjectDocCatalogs) {
			tempList.add(bmsProjectDocCatalog.getGuid());
		}
		for (Iterator<BmsProjectDocCatalog> iterator = bmsProjectDocCatalogs.iterator(); iterator.hasNext();) {
			BmsProjectDocCatalog bmsProjectDocCatalog = (BmsProjectDocCatalog) iterator.next();
			// 如果是顶级节点, 遍历该父节点的所有子节点
			if (!tempList.contains(bmsProjectDocCatalog.getParentId())) {
				
				recursionFn(bmsProjectDocCatalogs, bmsProjectDocCatalog);
				returnList.add(bmsProjectDocCatalog);
			}
		}
		if (returnList.isEmpty()) {
			returnList = bmsProjectDocCatalogs;
		}
		return returnList;
	}

	@Override
	public List<TreeSelectNew> buildDocCatalogTreeSelect(List<BmsProjectDocCatalog> bmsProjectDocCatalogs) {
		List<BmsProjectDocCatalog> bmsProjectDocCatalogTrees = buildDocCatalogTree(bmsProjectDocCatalogs);
		
		
		return bmsProjectDocCatalogTrees.stream().map(TreeSelectNew::new).collect(Collectors.toList());
	}

	/**
	 * 递归列表
	 */
	private void recursionFn(List<BmsProjectDocCatalog> list, BmsProjectDocCatalog t) {
		int counts = 0;
		// 得到子节点列表
		List<BmsProjectDocCatalog> childList = getChildList(list, t);
		t.setChildren(childList);
		for (BmsProjectDocCatalog tChild : childList) {
			
			if (hasChild(list, tChild)) {
				recursionFn(list, tChild);
				// 判断是否有子节点
				/*Iterator<BmsProjectDocCatalog> it = childList.iterator();
				while (it.hasNext()) {
					BmsProjectDocCatalog n = (BmsProjectDocCatalog) it.next();
					recursionFn(list, n);
				}*/
			}
			counts += tChild.getCounts();
		}
		t.setCounts(counts + t.getCounts());
	}

	/**
	 * 得到子节点列表
	 */
	private List<BmsProjectDocCatalog> getChildList(List<BmsProjectDocCatalog> list, BmsProjectDocCatalog t) {
		List<BmsProjectDocCatalog> tlist = new ArrayList<BmsProjectDocCatalog>();
		Iterator<BmsProjectDocCatalog> it = list.iterator();
		while (it.hasNext()) {
			BmsProjectDocCatalog n = (BmsProjectDocCatalog) it.next();
			if("0".equals(n.getDelFlag())) {
				if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().equals(t.getGuid())) {
					tlist.add(n);
				}
			}
			
		}
		return tlist;
	}

	/**
	 * 判断是否有子节点
	 */
	private boolean hasChild(List<BmsProjectDocCatalog> list, BmsProjectDocCatalog t) {
		return getChildList(list, t).size() > 0 ? true : false;
	}
	
	public void insertBmsProjectDocCatalog(List<TreeSelectExport> items,TreeSelectExport item,String guid,String createBy) {
		
		if(items != null && items.size()>0) {
			items.forEach(it ->{
				BmsProjectDocCatalog bmsProjectDocCatalog = new BmsProjectDocCatalog();
				bmsProjectDocCatalog.setName(it.getLabel());
				bmsProjectDocCatalog.setSort(it.getSort());
				bmsProjectDocCatalog.setBusinessId(it.getBusinessId());
				bmsProjectDocCatalog.setParentId(guid);
				bmsProjectDocCatalog.setCreateBy(createBy);
				bmsProjectDocCatalogMapper.insertBmsProjectDocCatalog(bmsProjectDocCatalog);
				
				if(it.getChildren() != null && it.getChildren().size()>0) {
					insertBmsProjectDocCatalog(it.getChildren(),it,bmsProjectDocCatalog.getGuid(),createBy);
				}
			});
		}
		
		
	}
	
	@Override
	public int importBmsProjectDocCatalogsList(List<BmsProjectDocCatalog> bmsProjectDocCatalogs) {
		String createBy = tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getUserId();
		List<BmsProjectDocCatalog> bmsProjectDocCatalogTrees = buildDocCatalogTreeExport(bmsProjectDocCatalogs);
		List<TreeSelectExport> res = bmsProjectDocCatalogTrees.stream().map(TreeSelectExport::new).collect(Collectors.toList());
		
		if(res!=null && res.size()>0) {
			BmsProjectDocCatalog docCatalog = new BmsProjectDocCatalog();
			docCatalog.setBusinessId(res.get(0).getBusinessId());
			docCatalog.setParentId("0");
			List<BmsProjectDocCatalog> resultDoc = bmsProjectDocCatalogMapper.selectBmsProjectDocCatalogList(docCatalog);
			if(resultDoc!=null && resultDoc.size()>0) {
				res.forEach(item ->{
					BmsProjectDocCatalog bmsProjectDocCatalog = new BmsProjectDocCatalog();
					bmsProjectDocCatalog.setName(item.getLabel());
					bmsProjectDocCatalog.setSort(item.getSort());
					bmsProjectDocCatalog.setBusinessId(item.getBusinessId());
					bmsProjectDocCatalog.setParentId(resultDoc.get(0).getGuid());
					bmsProjectDocCatalog.setCreateBy(createBy);
					bmsProjectDocCatalogMapper.insertBmsProjectDocCatalog(bmsProjectDocCatalog);
					if(item.getChildren()!=null && item.getChildren().size()>0) {
						insertBmsProjectDocCatalog(item.getChildren(),item,bmsProjectDocCatalog.getGuid(),createBy);
					}
				});
			}
			
		}
		return 1;
	}
	
	public List<BmsProjectDocCatalog> buildDocCatalogTreeExport(List<BmsProjectDocCatalog> bmsProjectDocCatalogs) {
		List<BmsProjectDocCatalog> returnList = new ArrayList<BmsProjectDocCatalog>();
		List<String> tempList = new ArrayList<String>();
		for (BmsProjectDocCatalog bmsProjectDocCatalog : bmsProjectDocCatalogs) {
			tempList.add(bmsProjectDocCatalog.getName());
		}
		for (Iterator<BmsProjectDocCatalog> iterator = bmsProjectDocCatalogs.iterator(); iterator.hasNext();) {
			BmsProjectDocCatalog bmsProjectDocCatalog = (BmsProjectDocCatalog) iterator.next();
			// 如果是顶级节点, 遍历该父节点的所有子节点
			if (!tempList.contains(bmsProjectDocCatalog.getParentName())) {
				recursionFnExport(bmsProjectDocCatalogs, bmsProjectDocCatalog);
				returnList.add(bmsProjectDocCatalog);
			}
		}
		if (returnList.isEmpty()) {
			returnList = bmsProjectDocCatalogs;
		}
		return returnList;
	}
	
	/**
	 * 递归列表
	 */
	private void recursionFnExport(List<BmsProjectDocCatalog> list, BmsProjectDocCatalog t) {
		// 得到子节点列表
		List<BmsProjectDocCatalog> childList = getChildListExport(list, t);
		t.setChildren(childList);
		for (BmsProjectDocCatalog tChild : childList) {
			if (hasChildExport(list, tChild)) {
				// 判断是否有子节点
				Iterator<BmsProjectDocCatalog> it = childList.iterator();
				while (it.hasNext()) {
					BmsProjectDocCatalog n = (BmsProjectDocCatalog) it.next();
					recursionFnExport(list, n);
				}
			}
		}
	}

	/**
	 * 得到子节点列表
	 */
	private List<BmsProjectDocCatalog> getChildListExport(List<BmsProjectDocCatalog> list, BmsProjectDocCatalog t) {
		List<BmsProjectDocCatalog> tlist = new ArrayList<BmsProjectDocCatalog>();
		Iterator<BmsProjectDocCatalog> it = list.iterator();
		while (it.hasNext()) {
			BmsProjectDocCatalog n = (BmsProjectDocCatalog) it.next();
			if (StringUtils.isNotNull(n.getParentName()) && n.getParentName().equals(t.getName())) {
				tlist.add(n);
			}
		}
		return tlist;
	}

	/**
	 * 判断是否有子节点
	 */
	private boolean hasChildExport(List<BmsProjectDocCatalog> list, BmsProjectDocCatalog t) {
		return getChildList(list, t).size() > 0 ? true : false;
	}

	@Override
	public int importBmsProjectDocCatalogs(List<ImportBmsProjectDocCatalog> bmsProjectDocCatalogs, String businessId) {
		Map<String, Object> parentDocCatalog = new HashMap<String, Object>();
		Map<String, Object> secondDocCatalog = new HashMap<String, Object>();
		Map<String, Object> thirdDocCatalog = new HashMap<String, Object>();
		Map<String, Object> fourthDocCatalog = new HashMap<String, Object>();
		Map<String, Object> fifthDocCatalog = new HashMap<String, Object>();

		BmsProjectDocCatalog docCatalog = new BmsProjectDocCatalog();
		docCatalog.setBusinessId(businessId);
		docCatalog.setParentId("0");
		List<BmsProjectDocCatalog> resultDoc = bmsProjectDocCatalogMapper.selectBmsProjectDocCatalogList(docCatalog);
		
		int result = 0;
		// 保存一级目录
		for (ImportBmsProjectDocCatalog importBmsProjectDocCatalog : bmsProjectDocCatalogs) {
			if (StringUtils.isNotBlank(importBmsProjectDocCatalog.getFirstName())
					&& StringUtils.isBlank(importBmsProjectDocCatalog.getSecondName())
					&& StringUtils.isBlank(importBmsProjectDocCatalog.getThirdName())
					&& StringUtils.isBlank(importBmsProjectDocCatalog.getFourthName())
					&& StringUtils.isBlank(importBmsProjectDocCatalog.getFifthName())
					&& StringUtils.isBlank(importBmsProjectDocCatalog.getSixthName())) {

				BmsProjectDocCatalog bmsProjectDocCatalog = new BmsProjectDocCatalog();

				bmsProjectDocCatalog.setName(importBmsProjectDocCatalog.getFirstName());
				bmsProjectDocCatalog
						.setCreateBy(tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getUserId());
				bmsProjectDocCatalog.setParentId(resultDoc.get(0).getGuid());
				bmsProjectDocCatalog.setBusinessId(businessId);

				bmsProjectDocCatalogMapper.insertBmsProjectDocCatalog(bmsProjectDocCatalog);

				parentDocCatalog.put(importBmsProjectDocCatalog.getFirstName(), bmsProjectDocCatalog.getGuid());
			} else {
				result = -1;
			}
		}

		// 保存二级目录
		for (ImportBmsProjectDocCatalog importBmsProjectDocCatalog : bmsProjectDocCatalogs) {
			if (!parentDocCatalog.isEmpty()) {
				if (StringUtils.isNotBlank(importBmsProjectDocCatalog.getFirstName())
						&& StringUtils.isNotBlank(importBmsProjectDocCatalog.getSecondName())
						&& StringUtils.isBlank(importBmsProjectDocCatalog.getThirdName())
						&& StringUtils.isBlank(importBmsProjectDocCatalog.getFourthName())
						&& StringUtils.isBlank(importBmsProjectDocCatalog.getFifthName())
						&& StringUtils.isBlank(importBmsProjectDocCatalog.getSixthName())) {

					if (parentDocCatalog.containsKey(importBmsProjectDocCatalog.getFirstName())) {

						BmsProjectDocCatalog bmsProjectDocCatalog = new BmsProjectDocCatalog();

						bmsProjectDocCatalog.setName(importBmsProjectDocCatalog.getSecondName());
						bmsProjectDocCatalog.setCreateBy(
								tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getUserId());
						bmsProjectDocCatalog.setParentId(
								parentDocCatalog.get(importBmsProjectDocCatalog.getFirstName()).toString());
						bmsProjectDocCatalog.setBusinessId(businessId);

						bmsProjectDocCatalogMapper.insertBmsProjectDocCatalog(bmsProjectDocCatalog);

						secondDocCatalog.put(importBmsProjectDocCatalog.getFirstName() + ";"
								+ importBmsProjectDocCatalog.getSecondName(), bmsProjectDocCatalog.getGuid());
					}

				}
			}
		}

		// 保存三级目录
		for (ImportBmsProjectDocCatalog importBmsProjectDocCatalog : bmsProjectDocCatalogs) {
			if (!secondDocCatalog.isEmpty()) {
				if (StringUtils.isNotBlank(importBmsProjectDocCatalog.getFirstName())
						&& StringUtils.isNotBlank(importBmsProjectDocCatalog.getSecondName())
						&& StringUtils.isNotBlank(importBmsProjectDocCatalog.getThirdName())
						&& StringUtils.isBlank(importBmsProjectDocCatalog.getFourthName())
						&& StringUtils.isBlank(importBmsProjectDocCatalog.getFifthName())
						&& StringUtils.isBlank(importBmsProjectDocCatalog.getSixthName())) {

					if (secondDocCatalog.containsKey(importBmsProjectDocCatalog.getFirstName() + ";"
							+ importBmsProjectDocCatalog.getSecondName())) {

						BmsProjectDocCatalog bmsProjectDocCatalog = new BmsProjectDocCatalog();

						bmsProjectDocCatalog.setName(importBmsProjectDocCatalog.getThirdName());
						bmsProjectDocCatalog.setCreateBy(
								tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getUserId());
						bmsProjectDocCatalog.setParentId(secondDocCatalog.get(importBmsProjectDocCatalog.getFirstName()
								+ ";" + importBmsProjectDocCatalog.getSecondName()).toString());
						bmsProjectDocCatalog.setBusinessId(businessId);

						bmsProjectDocCatalogMapper.insertBmsProjectDocCatalog(bmsProjectDocCatalog);

						thirdDocCatalog.put(importBmsProjectDocCatalog.getFirstName() + ";"
								+ importBmsProjectDocCatalog.getSecondName() + ";"
								+ importBmsProjectDocCatalog.getThirdName(), bmsProjectDocCatalog.getGuid());
					}

				}
			}
		}

		// 保存四级目录
		for (ImportBmsProjectDocCatalog importBmsProjectDocCatalog : bmsProjectDocCatalogs) {
			if (!thirdDocCatalog.isEmpty()) {
				if (StringUtils.isNotBlank(importBmsProjectDocCatalog.getFirstName())
						&& StringUtils.isNotBlank(importBmsProjectDocCatalog.getSecondName())
						&& StringUtils.isNotBlank(importBmsProjectDocCatalog.getThirdName())
						&& StringUtils.isNotBlank(importBmsProjectDocCatalog.getFourthName())
						&& StringUtils.isBlank(importBmsProjectDocCatalog.getFifthName())
						&& StringUtils.isBlank(importBmsProjectDocCatalog.getSixthName())) {

					if (thirdDocCatalog.containsKey(
							importBmsProjectDocCatalog.getFirstName() + ";" + importBmsProjectDocCatalog.getSecondName()
									+ ";" + importBmsProjectDocCatalog.getThirdName())) {

						BmsProjectDocCatalog bmsProjectDocCatalog = new BmsProjectDocCatalog();

						bmsProjectDocCatalog.setName(importBmsProjectDocCatalog.getFourthName());
						bmsProjectDocCatalog.setCreateBy(
								tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getUserId());
						bmsProjectDocCatalog.setParentId(thirdDocCatalog.get(importBmsProjectDocCatalog.getFirstName()
								+ ";" + importBmsProjectDocCatalog.getSecondName() + ";"
								+ importBmsProjectDocCatalog.getThirdName()).toString());
						bmsProjectDocCatalog.setBusinessId(businessId);

						bmsProjectDocCatalogMapper.insertBmsProjectDocCatalog(bmsProjectDocCatalog);

						fourthDocCatalog.put(importBmsProjectDocCatalog.getFirstName() + ";"
								+ importBmsProjectDocCatalog.getSecondName() + ";"
								+ importBmsProjectDocCatalog.getThirdName() + ";"
								+ importBmsProjectDocCatalog.getFourthName(), bmsProjectDocCatalog.getGuid());
					}

				}
			}
		}

		// 保存五级目录
		for (ImportBmsProjectDocCatalog importBmsProjectDocCatalog : bmsProjectDocCatalogs) {
			if (!fourthDocCatalog.isEmpty()) {
				if (StringUtils.isNotBlank(importBmsProjectDocCatalog.getFirstName())
						&& StringUtils.isNotBlank(importBmsProjectDocCatalog.getSecondName())
						&& StringUtils.isNotBlank(importBmsProjectDocCatalog.getThirdName())
						&& StringUtils.isNotBlank(importBmsProjectDocCatalog.getFourthName())
						&& StringUtils.isNotBlank(importBmsProjectDocCatalog.getFifthName())
						&& StringUtils.isBlank(importBmsProjectDocCatalog.getSixthName())) {

					if (fourthDocCatalog.containsKey(
							importBmsProjectDocCatalog.getFirstName() + ";" + importBmsProjectDocCatalog.getSecondName()
									+ ";" + importBmsProjectDocCatalog.getThirdName() + ";"
									+ importBmsProjectDocCatalog.getFourthName())) {

						BmsProjectDocCatalog bmsProjectDocCatalog = new BmsProjectDocCatalog();

						bmsProjectDocCatalog.setName(importBmsProjectDocCatalog.getFifthName());
						bmsProjectDocCatalog.setCreateBy(
								tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getUserId());
						bmsProjectDocCatalog.setParentId(fourthDocCatalog.get(importBmsProjectDocCatalog.getFirstName()
								+ ";" + importBmsProjectDocCatalog.getSecondName() + ";"
								+ importBmsProjectDocCatalog.getThirdName() + ";"
								+ importBmsProjectDocCatalog.getFourthName()).toString());
						bmsProjectDocCatalog.setBusinessId(businessId);

						bmsProjectDocCatalogMapper.insertBmsProjectDocCatalog(bmsProjectDocCatalog);

						fifthDocCatalog.put(importBmsProjectDocCatalog.getFirstName() + ";"
								+ importBmsProjectDocCatalog.getSecondName() + ";"
								+ importBmsProjectDocCatalog.getThirdName() + ";"
								+ importBmsProjectDocCatalog.getFourthName() + ";"
								+ importBmsProjectDocCatalog.getFifthName(), bmsProjectDocCatalog.getGuid());
					}

				}
			}
		}

		// 保存六级目录
		for (ImportBmsProjectDocCatalog importBmsProjectDocCatalog : bmsProjectDocCatalogs) {
			if (!fifthDocCatalog.isEmpty()) {
				if (StringUtils.isNotBlank(importBmsProjectDocCatalog.getFirstName())
						&& StringUtils.isNotBlank(importBmsProjectDocCatalog.getSecondName())
						&& StringUtils.isNotBlank(importBmsProjectDocCatalog.getThirdName())
						&& StringUtils.isNotBlank(importBmsProjectDocCatalog.getFourthName())
						&& StringUtils.isNotBlank(importBmsProjectDocCatalog.getFifthName())
						&& StringUtils.isNotBlank(importBmsProjectDocCatalog.getSixthName())) {

					if (fifthDocCatalog.containsKey(
							importBmsProjectDocCatalog.getFirstName() + ";" + importBmsProjectDocCatalog.getSecondName()
									+ ";" + importBmsProjectDocCatalog.getThirdName() + ";"
									+ importBmsProjectDocCatalog.getFourthName() + ";"
									+ importBmsProjectDocCatalog.getFifthName())) {
						BmsProjectDocCatalog bmsProjectDocCatalog = new BmsProjectDocCatalog();
						bmsProjectDocCatalog.setName(importBmsProjectDocCatalog.getSixthName());
						bmsProjectDocCatalog.setCreateBy(
								tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getUserId());
						bmsProjectDocCatalog.setParentId(fifthDocCatalog.get(importBmsProjectDocCatalog.getFirstName()
								+ ";" + importBmsProjectDocCatalog.getSecondName() + ";"
								+ importBmsProjectDocCatalog.getThirdName() + ";"
								+ importBmsProjectDocCatalog.getFourthName() + ";"
								+ importBmsProjectDocCatalog.getFifthName()).toString());
						bmsProjectDocCatalog.setBusinessId(businessId);

						bmsProjectDocCatalogMapper.insertBmsProjectDocCatalog(bmsProjectDocCatalog);
					}

				}
			}
		}
		return result;
	}

	@Override
	public int checkDocCatalog(String businessId, String isEdit) {
		return bmsProjectDocCatalogMapper.checkDocCatalog(businessId,isEdit);
	}

	@Override
	public List<BmsProjectDocCatalog> selectBmsProjectDocCatalogExportList(BmsProjectDocCatalog bmsProjectDocCatalog) {
		return bmsProjectDocCatalogMapper.selectBmsProjectDocCatalogExportList(bmsProjectDocCatalog);
	}

	@Override
	public void addDocCatalog(String businessId, String flag) {
		String name = "";
		double sort = new Double(9999);
		int isFlag = bmsProjectDocCatalogMapper.checkDocCatalog(businessId,flag);
		
		if(CatalogType.ZERO.getCode().equals(flag)) {
			name = CatalogName.PICKUP.getName();
			sort = new Double(9997);
		}else if(CatalogType.TWO.getCode().equals(flag)) {
			name = CatalogName.ARCHIVE.getName();
			sort = new Double(9999);
		}else if(CatalogType.THREE.getCode().equals(flag)) {
			name = CatalogName.RESULTREVIEW.getName();
			sort = new Double(9998);
		}
		// 1、首先判断项目文档中第二级目录有没有收资管理目录
		// 1.） 如果没有，则在项目文档中添加第二级目录收资管理目录
		// 2.） 如果有，则不需要任何操作
		if (isFlag <= 0) {
			BmsProjectDocCatalog bmsProjectDocCatalog = new BmsProjectDocCatalog();
			bmsProjectDocCatalog.setBusinessId(businessId);
			bmsProjectDocCatalog.setParentId("0");
			List<BmsProjectDocCatalog> docCatalogs = bmsProjectDocCatalogMapper
					.selectBmsProjectDocCatalogList(bmsProjectDocCatalog);
			if (docCatalogs != null && docCatalogs.size() > 0) {
				BmsProjectDocCatalog docCatalog = new BmsProjectDocCatalog();
				docCatalog.setParentId(docCatalogs.get(0).getGuid());
				docCatalog.setName(name);
				docCatalog.setSort(sort);
				docCatalog.setBusinessId(businessId);
				docCatalog.setIsEdit(flag);
				bmsProjectDocCatalogMapper.insertBmsProjectDocCatalog(docCatalog);
			}
		}
	
		
		
	}

	@Override
	public String selectDocCatalog(String businessId, String isEdit) {
		return bmsProjectDocCatalogMapper.selectDocCatalog(businessId, isEdit);
	}

	@Override
	public BmsProjectDocCatalog selectCatalogByParentIdAndName(String parentId, String name) {
		return bmsProjectDocCatalogMapper.selectCatalogByParentIdAndName(parentId, name);
	}

	@Override
	public Integer selectDocCounts(String catalogId) {
		return bmsProjectDocCatalogMapper.selectDocCounts(catalogId);
	}

	@Override
	public Integer selectAttachmentsCounts(String catalogId) {
		return bmsProjectDocCatalogMapper.selectAttachmentsCounts(catalogId);
	}

	@Override
	public Integer selectPickUpCounts(String businessId) {
		return bmsProjectDocCatalogMapper.selectPickUpCounts(businessId);
	}

	@Override
	public Integer selectArchiveCounts(String businessId) {
		return bmsProjectDocCatalogMapper.selectArchiveCounts(businessId);
	}

	@Override
	public Integer selectResultCounts(String catalogId) {
		return bmsProjectDocCatalogMapper.selectResultCounts(catalogId);
	}

	@Override
	public Integer selectPickUpSimpleCounts(String businessId) {
		// TODO Auto-generated method stub
		return bmsProjectDocCatalogMapper.selectPickUpSimpleCounts(businessId);
	}

	@Override
	public Integer selectArchiveSimpleCounts(String businessId) {
		// TODO Auto-generated method stub
		return bmsProjectDocCatalogMapper.selectArchiveSimpleCounts(businessId);
	}

	@Override
	public Integer selectResultSimpleCounts(String catalogId) {
		// TODO Auto-generated method stub
		return bmsProjectDocCatalogMapper.selectResultSimpleCounts(catalogId);
	}

	@Override
	public Integer selectDocSimpleCounts(String guid) {
		return bmsProjectDocCatalogMapper.selectDocSimpleCounts(guid);
	}

    /**
     * 构建树结构
     * @param bmsProjectDocCatalogs
     * @return
     */
	@Override
	public List<TreeSelectProjectDoc> buildDocCatalogTreeSelectProjectDoc(List<BmsProjectDocCatalog> bmsProjectDocCatalogs) {
		List<BmsProjectDocCatalog> bmsProjectDocCatalogTrees = buildDocCatalogTree(bmsProjectDocCatalogs);
		return bmsProjectDocCatalogTrees.stream().map(TreeSelectProjectDoc::new).collect(Collectors.toList());
	}
}
